import { httpClient } from '@/service/net/http-client'
import type { FeedList, Model, ModelCategory, ModelList } from '../types'

type FeedListGetterParams = {
  page_num: number
  page_size: number
  show_nsfw: boolean
}
export const feedListGetter = (params: FeedListGetterParams) => {
  const ins = httpClient.Get('/spell/feed/query/v2', {
    params,
    transformData(rawData: FeedList) {
      const list = []
      for (let i = 0; i < rawData.data.length; i++) {
        const item = rawData.data[i]
        list.push({
          id: item.feed_id,
          userName: item.user_name,
          customPrompt: item.custom_prompt,
          coverImageUrl: item.cover_image_url,
          userIcon: item.user_icon,
          width: item.width,
          height: item.height,
          tags: item.tags,
          feedTags: item.feed_tags,
          negativePrompt: item.negative_prompt
        })
      }
      return { ...rawData, data: list }
    }
  })
  return ins
}

/**
 * 获取多个模型详情
 * @param modelCodes 模型 code 列表
 * @returns 返回模型详情
 */
export const modelDetailGetter = (modelCodes: string[]) => {
  return httpClient.Get<Model[]>(
    `/spell/model/details?model_codes=${modelCodes.join('&model_codes=')}`
  )
}

/**
 * 获取模型分类列表
 * @param mode 模式：默认为 GENERAL
 * @returns 返回模型分类列表
 */
export const modelCategoriesGetter = (mode: string = 'GENERAL') => {
  const params = { mode }
  return httpClient.Get(`/spell/model_categories`, {
    params,
    transformData(rawData: ModelCategory[], headers) {
      if(!rawData) return []
      const arr = rawData.map((item) => {
        const opacity = item.category_color?.substring(1, 3)
        const color = `#${item.category_color?.slice(3)}${opacity}`
        return { ...item, category_color: color }
      })
      return arr
    }
  })
}

export type ModelsGetterParams = {
  page_size: number
  page_num: number
  category_code: string
}
export const modelsGetter = (params: ModelsGetterParams) => {
  return httpClient.Get<ModelList>('/spell/models/recommend', {
    params: { ...params, mode: 'GENERAL' }
  })
}
